<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ToolLogic extends Controller
{
    private $allow_file = [
        'video/wmv',
        'video/avi',
        'video/mpg',
        'video/mpeg',
        'video/3gp',
        'video/mov',
        'video/mp4',
        'video/flv',
        'video/f4v',
        'video/m4v',
        'video/m2t',
        'video/mts',
        'video/rmvb',
        'video/vob',
        'video/mkv',
        'jpeg',
        'bmp',
        'png',
        'xls',
        'xlsx',
        'ppt',
        'pptx',
        'doc',
        'docx',
        'gif',
    ];

    //上传文件
    public function uploadFile($request)
    {
        try {
            ini_set('max_execution_time', '1200');
            ini_set('max_input_time ', '1200');
            ini_set('upload_max_filesize ', '200M');
            ini_set('post_max_size', '200M');
            $file = $request->file();
            $file_key = array_keys($file);
            $allow_file = implode(',', $this->allow_file);
            $rules = [];
            for ($i = 0; $i < count($file_key); $i++) {
                $rules[$file_key[$i]] = 'required|mimes:'. $allow_file;
            }

            $this->verify($file, $rules);
            $dir = $request->input('dir');
            $result = [];
            foreach ($file_key as $value) {
                $context = stream_context_create(array('http'=>array('ignore_errors'=>true)));
                $contents = file_get_contents($file[$value], FALSE, $context);
                $path = Storage::putFile('public/' . $dir . '/' . date('Y-m-d'), $file[$value]);
                $path = str_replace('public', 'storage', $path);
                $url = cdnImagePath($path);
                $result[$value] = $url;
            }
            return $result;
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }


}
